﻿//import "~/_content/HiNcRcl/lib/ace/ace.min.js";
//import "~/_content/HiNcRcl/lib/ace/ext-language_tools.min.js";
//import "~/_content/HiNcRcl/lib/ace/theme-textmate.min.js";
//import "~/_content/HiNcRcl/lib/ace/mode-csharp.min.js";
import "../../lib/ace/ace.min.js";
import "../../lib/ace/ext-language_tools.min.js";
import "../../lib/ace/theme-textmate.min.js";
import "../../lib/ace/mode-csharp.min.js";

export function initEditor(domEditorDiv, csThis, editorValue, completions) {
    //console.log(completions);
    if (domEditorDiv == null)
        return;
    let langTools = ace.require("ace/ext/language_tools");
    var editor = ace.edit(domEditorDiv);
    domEditorDiv.editor = editor;
    //console.log('initEditor');
    //console.log(editorValue);
    //console.log(editor);
    //console.log(editor.getSession().getValue());
    editor.textInput.getElement().id = "ace-textarea-" + domEditorDiv.id;

    editor.session.setMode("ace/mode/csharp");
    editor.setTheme("ace/theme/textmate");
    // enable autocompletion and snippets
    editor.setOptions({
        enableBasicAutocompletion: true,
        enableSnippets: true,
        enableLiveAutocompletion: true,
        fontSize: "100%"
    });
    editor.on('input', function () {
        //console.log('input');
        //console.log(editor);
        //console.log(editor.getSession());
        //console.log(editor.getSession().getValue());
        csThis.invokeMethodAsync('SetEditorText', editor.getSession().getValue());
    });

    var rhymeCompleter = {
        getCompletions: function (editor, session, pos, prefix, callback) {
            //Region C#
            var wordList = ["yield", "foreach"];
            callback(null, wordList.map(function (word) {
                return {
                    caption: word,
                    value: word,
                    meta: "keyword"
                };
            }));

            var wordList = ["IEnumerable"];
            callback(null, wordList.map(function (word) {
                return {
                    caption: word,
                    value: word,
                    meta: "class"
                };
            }));

            callback(null, completions);
        },
        getDocTooltip: function (item) {
            //console.log('getDocTooltip.item', item);
            if (item.meta == 'property' || item.meta == 'function') {
                item.docHTML = [
                    "<b>", item.caption, "</b>", " - ", item.classExt,
                    "<hr></hr>",
                    item.docContentHtml
                ].join("");
            }
        }
    }
    langTools.addCompleter(rhymeCompleter);

    //set completer width. the startAutocomplete has to be exec first to generate popup container.
    if (!editor.completer) {
        editor.execCommand("startAutocomplete")
        editor.completer.detach()
    }
    editor.completer.popup.container.style.width = "40%"

    editor.getSession().setValue(editorValue);
    return editor;
}
export function dispose(domEditorDiv) {
    if (domEditorDiv == null)
        return;
    domEditorDiv.editor.on('input', null);
}
export function setEditorValue(domEditorDiv, text) {
    if (domEditorDiv == null)
        return;
    let session = domEditorDiv.editor.getSession();
    if (session != null)
        session.setValue(text);
}

export function setEditorReadOnly(domEditorDiv, b) {
    if (domEditorDiv == null)
        return;
    let editor = domEditorDiv.editor;
    if (b)
        editor.container.classList.add("bg-secondary-subtle");
    else
        editor.container.classList.remove("bg-secondary-subtle");
    editor.setReadOnly(b);
}